package uml.eventsystem;


import java.util.ArrayList;
import java.util.EnumMap;

import uml.eventsystem.events.UMLEventId;

/**
 * Implementation of the ISubject interface.
 */
public class SubjectImpl
		implements ISubject
{
	private EnumMap<UMLEventId, ArrayList<IObserver>> m_observerMap;

	/**
	 * Constructor.
	 */
	public SubjectImpl ()
	{
		m_observerMap = new EnumMap<UMLEventId, ArrayList<IObserver>> (UMLEventId.class);

		// Initialize all event IDs with empty lists
		for (UMLEventId ueId : UMLEventId.values ())
		{
			m_observerMap.put (ueId, new ArrayList<IObserver> ());
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void addObserver (IObserver observer, UMLEventId eventId)
	{
		ArrayList<IObserver> listeners = m_observerMap.get (eventId);
		listeners.add (observer);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void removeObserver (IObserver observer, UMLEventId eventId)
	{
		ArrayList<IObserver> observerList = m_observerMap.get (eventId);
		observerList.remove (observer);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void notifyObservers (UMLEventId eventId)
	{
		ArrayList<IObserver> observerList = m_observerMap.get (eventId);
		for (IObserver observer : observerList)
		{
			observer.handleNotification (eventId);
		}
	}
}
